home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 February (DVD) / PCWorld_2008-02_DVD.iso / v cisle / PHP / PHP.exe / EasyPHP-2.0b1-setup.exe / {app} / sqlitemanager / include / SQLiteViewProperties.class.php < prev    next >
Encoding:
PHP Script  |  2006-04-18  |  7.1 KB  |  217 lines

  1. <?php
  2. /**
  3. * Web based SQLite management
  4. * View management Class
  5. * @package SQLiteManager
  6. * @author FrΘdΘric HENNINOT
  7. * @version $Id: SQLiteViewProperties.class.php,v 1.31 2006/04/16 18:56:57 freddy78 Exp $ $Revision: 1.31 $
  8. */
  9.  
  10. class SQLiteViewProperties {
  11.  
  12.     /**
  13.     * reference of connection object
  14.     *
  15.     * @access public
  16.     * @var object
  17.     */
  18.     var $connId;
  19.     
  20.     /**
  21.     * VIEW name
  22.     *
  23.     * @access private
  24.     * @var string
  25.     */
  26.     var $view;
  27.     
  28.     /**
  29.     * this VIEW is exist?
  30.     *
  31.     * @access private
  32.     * @var bool
  33.     */
  34.     var $isExist;
  35.     
  36.     /**
  37.     * Properties of current VIEW
  38.     *
  39.     * @access private
  40.     * @var array
  41.     */
  42.     var $viewProperties;
  43.     
  44.     /**
  45.     * Class constructor
  46.     *
  47.     * @access public
  48.     * @param object $conn reference to the connection object
  49.     */
  50.     function SQLiteViewProperties($conn){
  51.         // constructeur de la classe
  52.         $this->connId = $conn;
  53.         if($GLOBALS["view"] && ($GLOBALS["action"]!="add")) {
  54.             $this->view = $GLOBALS["view"];
  55.         } elseif($GLOBALS["ViewName"]){
  56.             $this->view = $GLOBALS["ViewName"];
  57.         } else return false;        
  58.         $this->isExist = $this->viewExist($this->view);
  59.         return $this->isExist;
  60.     }
  61.     
  62.     /**
  63.     * Verify if VIEW exist
  64.     *
  65.     * @access public
  66.     * @param string
  67.     */
  68.     function viewExist($view){
  69.         if(empty($view)) $view = $this->view;
  70.         $query = "SELECT sql FROM sqlite_master WHERE type='view' AND name=".quotes($view).";";
  71.         if($this->connId->getResId($query)){
  72.             $viewSQL = $this->connId->connId->fetch_single();
  73.             if(!empty($viewSQL)){
  74.                 $viewSQL = str_replace('select', 'SELECT', $viewSQL);    
  75.                 preg_match('/SELECT(.*)/', str_replace("\n", ' ', $viewSQL), $propTab);    
  76.                 $this->viewProperties = trim($propTab[0]);
  77.                 return true;
  78.             } else return false;
  79.         } else return false;            
  80.     }
  81.     
  82.     /**
  83.     * Save current view properties
  84.     *
  85.     * @access public
  86.     */
  87.     function saveProp(){
  88.         if(($GLOBALS['action']=='delete') || ($this->isExist)){
  89.             $queryDisplay = $query[] = 'DROP VIEW '.brackets($this->view).';';
  90.         }
  91.         if($GLOBALS['action']!='delete'){
  92.             if($this->isExist) $viewname = $this->view;
  93.             else $viewname = $_POST['ViewName'];
  94.             if(!empty($_POST['ViewName']) && !empty($_POST['ViewProp'])) {                
  95.                 $queryDisplay = $query[] = 'CREATE VIEW '.brackets($viewname).' AS '.urldecode(stripslashes($_POST['ViewProp'])).';';
  96.             }
  97.         }            
  98.         $errorMessage = '';
  99.         if($query){            
  100.             foreach($query as $req){
  101.                 $this->connId->getResId("BEGIN;");
  102.                 $res = $this->connId->getResId($req);
  103.                 $this->connId->getResId("COMMIT;");
  104.                 if(!$res){
  105.                     $errorCode = @sqlitem_last_error($this->connId->connId);
  106.                     $errorMessage .= $GLOBALS['traduct']->get(9).' '.$errorCode.' : '.@$this->connId->connId->getError().'\n';
  107.                 }
  108.             }
  109.         } else if(empty($_POST['ViewName']) || empty($_POST['ViewProp'])){
  110.             $errorMessage .= $GLOBALS['traduct']->get(18);
  111.         }
  112.         if($GLOBALS['action']!='delete') {
  113.             if($queryDisplay) displayQuery($queryDisplay);
  114.             if(!empty($errorMessage)){
  115.                 displayError($errorMessage);
  116.                 $this->viewEditForm();
  117.             }
  118.         } else {
  119.                 $GLOBALS["redirect"] = "<script type=\"text/javascript\">parent.left.location='left.php?dbsel=".$GLOBALS["dbsel"]."'; parent.main.location='main.php?dbsel=".$GLOBALS["dbsel"]."';</script>";
  120.         }
  121.     }
  122.  
  123.     /**
  124.     * Display current VIEW properties
  125.     *
  126.     * @access public
  127.     */
  128.     function propView(){
  129.       echo '<!-- SQLiteViewProperties.class.php : propView() -->'."\n";
  130.         echo '<br>';
  131.         echo '    <table cellpadding="2" cellspacing="0" width="90%" class="viewProp">
  132.                     <tr class="viewPropTitle"><td align="right" width="20%" class="viewPropTitle">'.$GLOBALS['traduct']->get(19).' : </td><td align="center" class="viewPropTitle">'.$this->view.'</td></tr>
  133.                     <tr><td align="right" class="viewProp">'.$GLOBALS['traduct']->get(61).' : </td><td class="viewProp">'.highlight_query($this->viewProperties).'</td></tr>
  134.                 </table>';
  135.         echo '<div align="center">';
  136.         if(!$GLOBALS['workDb']->isReadOnly() && displayCondition('properties')) echo '<a href="main.php?dbsel='.$GLOBALS['dbsel'].'&view='.$this->view.'&action=modify" class="base" target="main">'.$GLOBALS['traduct']->get(14).'</a>';
  137.         else echo '<span class="base"><i>'.$GLOBALS['traduct']->get(14).'</i></span>';
  138.         echo str_repeat(' ', 10);
  139.         if(!$GLOBALS['workDb']->isReadOnly() && displayCondition('del')) echo '<a href="main.php?dbsel='.$GLOBALS['dbsel'].'&view='.$this->view.'&action=delete" class="base" target="main">'.$GLOBALS['traduct']->get(15).'</a>';
  140.         else echo '<span class="base"><i>'.$GLOBALS['traduct']->get(15).'</i></span>';
  141.         echo '</div>';
  142.     }
  143.     
  144.     /**
  145.     * Display VIEW form to add or modify
  146.     *
  147.     * @access public
  148.     */
  149.     function viewEditForm(){
  150.       echo '<!-- SQLiteViewProperties.class.php : viewEditForm() -->'."\n";
  151.         echo '<br><center>';
  152.         if($GLOBALS['action']=='add') echo '<h4>'.$GLOBALS['traduct']->get(62).'</h4>';
  153.         else echo '<h4>'.$GLOBALS['traduct']->get(63).' : '.$this->view.'</h4>';
  154.         if($this->isExist){
  155.             $ViewName = $this->view;
  156.             $ViewProp = $this->viewProperties;
  157.         } else {
  158.             $ViewName = '';
  159.             $ViewProp = '';
  160.         }
  161.         echo '<form name="viewprop" action="main.php?dbsel='.$GLOBALS['dbsel'].'" method="POST" target="main">';
  162.         echo '    <table cellpadding="2" cellspacing="0" width="70%">
  163.                     <tr><td align="right" class="viewPropTitle" style="white-space: nowrap">'.$GLOBALS['traduct']->get(19).' :</td><td class="viewProp"><input type="text" class="text" name="ViewName" value="'.$ViewName.'"></td>
  164.                     <tr><td align="right" class="viewPropTitle" style="white-space: nowrap">'.$GLOBALS['traduct']->get(61).' :</td><td class="viewProp"><textarea name="ViewProp" cols="60" rows="4">'.htmlentities($ViewProp, ENT_NOQUOTES, $GLOBALS['charset']).'</textarea></td>
  165.                 </table>';
  166.         echo '<input type="hidden" name="view" value="'.$this->view.'">'."\n";
  167.         echo '<input type="hidden" name="action" value="save">'."\n";
  168.         echo '<input class="button" type="submit" value="'.$GLOBALS['traduct']->get(51).'">';
  169.         echo '</form>' . "\n";
  170.         echo '</center>';
  171.  
  172.     }
  173.  
  174.     /**
  175.     * Generate SQL query for 'select'
  176.     * @author Maurφcio M. Maia <mauricio.maia@gmail.com>
  177.     *
  178.     * @param string $table
  179.     */
  180.     function selectElement($view) {
  181.         $showField = $_REQUEST['showField'];
  182.         $valField = $_REQUEST['valField'];
  183.         $operats = $_REQUEST['operats'];
  184.         $error = false;
  185.         $selectQuery = 'SELECT ';
  186.         $condQuery = '';
  187.         if(is_array($_REQUEST['showField']) && !empty($_REQUEST['showField'])){
  188.             $selectQuery .= implode(", ", array_keys($_REQUEST['showField']));
  189.         } else $selectQuery .= '*';
  190.         
  191.         $selectQuery .= ' FROM '.brackets($view).' ';
  192.         if(is_array($_REQUEST['valField']) && !empty($_REQUEST['valField'])){
  193.             foreach($valField as $key => $value) {
  194.                 if (    (isset($value) && !empty($value)) 
  195.                         || (isset($operats[$key])
  196.                         && !empty($operats[$key]))) {
  197.     
  198.                     if($operats[$key] == 'ISNULL' || $operats[$key] == 'NOTNULL'){
  199.                         $condQuery .= $key.' '.$operats[$key];
  200.                     } else {
  201.                         $condQuery .= $key.' '.$operats[$key].' '.quotes($value);
  202.                     }
  203.                 }
  204.             }
  205.         }
  206.         if(!empty($_REQUEST['CondSuppl'])){
  207.             if($condQuery) $condQuery .= ' '.$_REQUEST['operSuppl'].' ';
  208.             $condQuery .= $_REQUEST['CondSuppl'];
  209.         }
  210.         return $selectQuery.(($condQuery)? 'WHERE '.$condQuery : '' );
  211.     }
  212.  
  213. }
  214.  
  215. ?>
  216.  
  217.